package cn.qingyun.gis.modules.scenes.controller;

import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.alongstreet.service.IAlongStreetProofreadService;
import cn.qingyun.gis.modules.express.response.RoleInfoResponse;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.scenes.entity.PoiScenario;
import cn.qingyun.gis.modules.scenes.request.*;
import cn.qingyun.gis.modules.scenes.response.AddressResponse;
import cn.qingyun.gis.modules.scenes.service.IPoiGovernmentService;
import cn.qingyun.gis.modules.scenes.service.IPoiProofreadService;
import cn.qingyun.gis.modules.scenes.service.IPoiScenarioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.TreeMap;


/**
 * @Description: 场景攻坚
 * @Version: V1.0
 */
@Api(tags = "场景攻坚")
@RequestMapping("/app/scenes/")
@RestController
@Slf4j
@Validated
public class PoiProofreadController extends BasicController<PoiScenario, IPoiScenarioService> {

    @Value("${qingyun.project.service-host:http://localhost:8080}")
    private String projectPath;

    @Resource
    private IPoiProofreadService iPoiProofreadService;

    @Resource
    private IPoiScenarioService iPoiScenarioService;
    @Resource
    private IAlongStreetProofreadService iAlongStreetProofreadService;

    @Resource
    private IPoiGovernmentService iPoiGovernmentService;

    @Deprecated
    @GetMapping("/poi/query")
    @ApiOperation("列表查询")
    public Result<?> poiQuery(@Validated PoiListRequest request) {
        return Result.okData(iPoiProofreadService.poiQueryNew(request));
    }

    @GetMapping("/poi/new/query")
    @ApiOperation("列表查询--改造")
    public Result<?> poiNewQuery(@Validated PoiListRequest request) {
        return Result.okData(iPoiProofreadService.poiQueryList(request));
    }

    @Deprecated
    @GetMapping("/noFollow/queryById")
    @ApiOperation("根据id + type 返回未随访poi信息")
    public Result<?> noFollowQueryById(@NotEmpty(message = "传入唯一标识[id]不能为空") String id,
                                       @NotEmpty(message = "传入场景类型[type]不能为空") String type) {
        return Result.okData(iPoiProofreadService.noFollowQueryById(id, type));
    }

    @Deprecated
    @GetMapping(value = "/follow/queryById")
    @ApiOperation("根据id + type 返回随访poi信息")
    public Result<?> queryTaskDetailById(@NotEmpty(message = "传入唯一标识[id]不能为空") String id,
                                         @NotEmpty(message = "传入场景类型[type]不能为空") String type) {
        return Result.okData(iPoiProofreadService.followQueryById(id, type));
    }

    @PostMapping("/proofread")
    @ApiOperation("站点随访")
    public Result<?> proofread(@RequestBody @Validated PoiKwRequest request) {
        return Result.okData(iPoiProofreadService.proofread(request));
    }

    @ApiOperation("摸排过程草稿记录")
    @PostMapping("/draft/proofread/saveOrUpdate")
    public Result<?> selectRoadNameStr(@Validated @RequestBody PoiDraftRequest request) {
        return Result.okData(iPoiProofreadService.draftProofreadSaveOrUpdate(request));
    }

    @GetMapping("/select/intention/record")
    @ApiOperation("历史意向业务查询")
    public Result<?> intentionRecordQuery(@Validated IntentionInfoRequest request) {
        return Result.okData(iPoiProofreadService.selectIntentionRecord(request));
    }

    @PostMapping("/upload/photo")
    @ApiOperation("上传图片")
    public Result<?> uploadPhoto(@RequestBody @Validated PhotoKwRequest request) {
        return Result.okData(iPoiProofreadService.uploadPhoto(request));
    }

    @PostMapping("/upload/photo/new")
    @ApiOperation("上传图片")
    public Result<?> uploadPhotoNew(@RequestBody @Validated PhotoKwRequest request) {
        return Result.okData(iPoiProofreadService.uploadPhotoNew(request));
    }

    @GetMapping("/photo/show")
    @ApiOperation("展示图片")
    public Result<?> showPhoto(@RequestParam("id") String id, @RequestParam("type") Integer type, @RequestParam("proofreadId") String proofreadId) {
        return Result.okData(iPoiProofreadService.showPhoto(id, type, proofreadId));
    }

    @GetMapping("/photo/query")
    @ApiOperation("政企建档查询图片")
    public Result<?> queryPhoto(@NotEmpty(message = "id不能为空") String id) {
        return Result.okData(iPoiProofreadService.queryPhoto(id));
    }

    @GetMapping("/getPath")
    @ApiOperation("获取项目路径")
    public Result<?> getPath() {
        return Result.okData(projectPath);
    }

    @GetMapping("/image/{imagePath}/{imageName}")
    @ApiOperation(value = "图片下载")
    public void downloadImage(HttpServletResponse response,
                              @PathVariable(value = "imagePath", required = false) @Validated @NotBlank(message = "图片路径不能为空！") String imagePath,
                              @PathVariable(value = "imageName", required = false) @Validated @NotBlank(message = "图片名称不能为空！") String imageName) {
        iAlongStreetProofreadService.downloadImage(response, String.format("%s/%s", imagePath, imageName));
    }


    @ApiOperation(value = "通过高德poi_id查询地址详情", notes = "通过高德poi_id查询地址详情")
    @GetMapping(value = "/queryAddress")
    public Result<?> queryAddressDetailByGdId(@RequestParam(name = "poiId") @NotEmpty(message = "高德[poiId]不能为空！") String poiId) {
        AddressResponse response = iPoiScenarioService.queryAddressDetailByGdId(poiId);
        if (response == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(response);
    }

    @ApiOperation(value = "录音识别", notes = "录音识别")
    @PostMapping(value = "/getSpeechText")
    public Result<?> getSpeechText(@Validated UploadTextRequest request) {
        return Result.okData(iPoiProofreadService.getSpeechText(request));
    }

    @ApiOperation(value = "语音识别", notes = "语音识别")
    @PostMapping(value = "/getSpeech")
    public Result<?> getSpeech(@RequestBody MultipartFile file) {
        return Result.okData(iPoiProofreadService.getSpeech(file));
    }

    @GetMapping("/file/{filePath}/{fileName}")
    @ApiOperation(value = "文件下载")
    public void downloadFile(HttpServletResponse response,
                             @PathVariable(value = "filePath", required = false) @Validated @NotBlank(message = "文件路径不能为空！") String filePath,
                             @PathVariable(value = "fileName", required = false) @Validated @NotBlank(message = "文件名称不能为空！") String fileName) {
        // todo: 后期改造
        iAlongStreetProofreadService.downloadImage(response, String.format("%s/%s", filePath, fileName));
    }

    @PostMapping("/file/deleteById")
    @ApiOperation(value = "文件删除")
    public Result<?> deleteFile(@RequestBody @Validated AudioDeleteRequest request) {
        iPoiProofreadService.deleteFile(request);
        return Result.ok("录音删除成功");
    }

    @Deprecated
    @GetMapping("/index/query")
    @ApiOperation(value = "首页搜索")
    public Result<?> indexQuery(@Validated IndexAroundRequest request) {
        return Result.okData(iPoiProofreadService.indexQuery(request));
    }

    @GetMapping("/index/new/query")
    @ApiOperation(value = "app首页可视化区域搜索改造")
    public Result<?> indexNewQuery(@Validated IndexAroundNewRequest request) {
        return Result.okData(iPoiProofreadService.indexNewQuery(request));
    }

    @Deprecated
    @GetMapping("/query/keyword")
    @ApiOperation(value = "六大场景-关键字搜索")
    public Result<?> poiQueryByWord(@Validated QueryByWordRequest request) {
        return Result.okData(iPoiProofreadService.poiQueryByWordNew(request));
    }

    @PostMapping("/query/new/keyword")
    @ApiOperation(value = "六大场景-关键字搜索新接口")
    public Result<?> poiQueryByWordNew(@Validated @RequestBody QueryByWordRequest request) {
        return Result.okData(iPoiProofreadService.poiSearchByWord(request));
    }

    @GetMapping("/detail/export")
    @ApiOperation(value = "导出明细")
    public void exportExcel(@NotEmpty(message = "区域编码不能为空") String code) {
        iPoiProofreadService.exportScenesExcel(code);
    }

    @PostMapping("/detail/exportNew")
    @ApiOperation(value = "导出明细--改造")
    public void exportExcelNew(@RequestBody @Validated PoiChannelRequest poiChannelRequest) {
        iPoiProofreadService.exportScenesExcelNew(poiChannelRequest);
    }

    @GetMapping("/excel/header")
    @ApiOperation(value = "管理后台---导出excel表头配置")
    public Result<?> exportExcelHeader() {
        return Result.okData(iPoiProofreadService.exportExcelHeader());
    }


    @ApiOperation(value = "随访明细表-分页列表查询", notes = "随访明细表-分页列表查询")
    @GetMapping(value = "/detailPage")
    public Result<?> selectDetailPage(@NotEmpty(message = "区域编码不能为空") String code,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      String kwPhone,
                                      Integer kwState) {
        return Result.okData(iPoiProofreadService.selectDetailPage(pageNo, pageSize, code, kwPhone, kwState));
    }

    @ApiOperation(value = "随访明细表-分页列表查询-新版", notes = "随访明细表-分页列表查询")
    @GetMapping(value = "/detailPageNew")
    public Result<?> selectDetailPageNew(@Validated PoiSearchRequest poiSearchRequest) {
        return Result.okData(iPoiProofreadService.selectDetailPageNew(poiSearchRequest));
    }

    @ApiOperation(value = "随访明细详情查询", notes = "随访明细详情查询")
    @GetMapping(value = "/poiInfo/detail")
    public Result<?> selectPoiInfoDetail(@Validated PoiInfoDetailRequest poiInfoDetailRequest) {
        return Result.okData(iPoiProofreadService.selectPoiInfoDetail(poiInfoDetailRequest));
    }

    @ApiOperation("获取默认最大权限数据")
    @GetMapping("/defaultPermissions")
    public Result<?> getDefaultData() {
        return Result.okData(iPoiProofreadService.getDefaultData());
    }

    @ApiOperation("根据手机号返回业务信息")
    @GetMapping("/businessByPhone")
    public Result<?> businessByPhone(@NotEmpty(message = "手机号码不能为空") String phone) {
        return Result.okData(iPoiProofreadService.businessByPhone(phone));
    }

    @ApiOperation("判断当前工号+渠道编码 判定是否有进入系统权限")
    @GetMapping("/checkPhone")
    public Result<?> checkPhone(@NotEmpty(message = "手机号码不能为空") String phone, String channelCode) {
        Boolean aBoolean = iPoiProofreadService.checkPhone(phone, channelCode);

        log.debug("判定是否有进入系统权限 {} {} {}", phone, channelCode, aBoolean);
        return Result.okData(aBoolean);
    }

    @ApiOperation("查询用户的角色列表")
    @PostMapping("/roleList")
    public Result<?> checkPhone(@RequestBody TreeMap<String, String> decryptedMap) {
        List<RoleInfoResponse> list = iPoiProofreadService.areaPermissionByNumNew(decryptedMap);
        return Result.okData(list);
    }
    @ApiOperation("查询直销用户的角色")
    @GetMapping("/roleInfo")
    public Result<?> selectRoleInfo(@NotEmpty(message = "用户工号不能为空！")@RequestParam("telNum") String telNum) {
        return Result.okData(iPoiProofreadService.selectRoleInfo(telNum));
    }

    @GetMapping("/model/export")
    @ApiOperation(value = "excel模板下载")
    public void excelModelDownload(HttpServletResponse response) {
        iPoiProofreadService.modelDownload(response);
    }

    @PostMapping("/people/import/verify")
    @ApiOperation(value = "责任人excel导入之前校验以及返回新增/更新条数")
    public Result<?> excelPeopleImportVerify(MultipartFile file) {
        return iPoiProofreadService.peopleExcelImportVerify(file);
    }

    @PostMapping("/people/import/confirm")
    @ApiOperation(value = "责任人excel导入")
    public Result<?> excelPeopleImport(MultipartFile file) {
        iPoiProofreadService.peopleExcelImport(file);
        return Result.ok("操作成功！");
    }

    @ApiOperation("扫描光猫返回的信息")
    @GetMapping("/scan/opticalModem")
    public Result<?> scanOpticalModem(@NotEmpty(message = "设备类型不能为空！") String devType,
                                      @NotEmpty(message = "设备串码不能为空！") String terminalNo) {
        return Result.okData(iPoiProofreadService.scanOpticalModem(devType, terminalNo));
    }

    @ApiOperation("首页统计信息接口")
    @PostMapping("/select/poiCount")
    public Result<?> selectPoiCount(@Validated @RequestBody PoiCountRequest poiCountRequest) {
        return Result.okData(iPoiProofreadService.selectPoiCount(poiCountRequest));
    }

    @ApiOperation("具体类型统计信息接口")
    @GetMapping("/select/specific/infoCount")
    public Result<?> selectSpecificPoiCount(@Validated PoiCountRequest request) {
        return Result.okData(iPoiProofreadService.selectSpecificPoiCount(request));
    }

    @ApiOperation("详情接口查询（企业+建筑）")
    @GetMapping("/select/poiDetail")
    public Result<?> selectSpecificPoiCount(@Validated PoiDetailRequest request) {
        return Result.okData(iPoiProofreadService.selectBuildDetailInfo(request));
    }

    @GetMapping("/poi/settled/query")
    @ApiOperation("入住企业列表查询")
    public Result<?> poiSettledQuery(@Validated PoiPageRequest request) {
        return Result.okData(iPoiProofreadService.poiSettledList(request));
    }

    @GetMapping("/build/detail/query")
    @ApiOperation("楼宇明细查询")
    public Result<?> buildDetailQuery(@Validated BuildDetailRequest request) {
        return Result.okData(iPoiProofreadService.buildingDetailQuery(request));
    }

    @ApiOperation("查询勘误的历史记录")
    @GetMapping("/select/KwRecord")
    public Result<?> selectKwPeopleRecord(@NotEmpty(message = "主键不能为空！") String id,
                                          @NotEmpty(message = "场景类型不能为空！") String type) {
        return Result.okData(iPoiProofreadService.selectKwPeopleRecord(id, type));
    }

    @ApiOperation("app页面聚合")
    @GetMapping("/index/query/together")
    public Result<?> selectIndexQueryTogether(@Validated IndexTogetherRequest indexTogetherRequest) {
        return Result.okData(iPoiProofreadService.selectIndexQueryTogetherData(indexTogetherRequest));
    }

    @ApiOperation("查询路巷街、门牌号")
    @GetMapping("/select/road/info")
    public Result<?> selectRoadNameStr(@Validated RoadInfoRequest roadInfoRequest) {
        return Result.okData(iPoiGovernmentService.selectRoadInfo(roadInfoRequest));
    }
}
